<?
/*
* ===========================================*
* 函数说明:查找关键字
* ===========================================*
*/
function GetKeyWord($domainName,$refererUrl)
{
	//国内的SE多是GB2312的格式

	if( strstr( $domainName, 'baidu.com') )
	{
		preg_match( "|baidu.+wo?r?d=([^\&]*)|is", $refererUrl, $tmp );
		$keyWord = urldecode( $tmp[1] );
	}
	else if( strstr( $domainName, 'google.') )
	{
		preg_match( "|google.+q=([^\&]*)|is", $refererUrl, $tmp );
		if( stristr( $refererUrl, 'gb2312' ) )
		{
			$keyWord = urldecode( $tmp[1] );
		}
		else
		{
			$keyWord = utf82gb2312( urldecode( $tmp[1] ) );
		}
	}
	else if( strstr( $domainName, 'sohu.com') )
	{
		preg_match( "|sohu.+query=([^\&]*)|is", $refererUrl, $tmp );
		$keyWord = urldecode( $tmp[1] );
	}
	else if( strstr( $domainName, 'sina.com.cn') )
	{
		preg_match( "|sina.+searchkey=([^\&]*)|is", $refererUrl, $tmp );
		$keyWord = urldecode( $tmp[1] );
	}
	else if( strstr( $domainName, '163.com') )
	{
		preg_match( "|163.+q=([^\&]*)|is", $refererUrl, $tmp );
		$keyWord = urldecode( $tmp[1] );
	}
	else if( strstr( $domainName, 'yahoo.com') )
	{
		preg_match( "|yahoo.+p=([^\&]*)|is", $refererUrl, $tmp );
		if( stristr( $refererUrl, 'gb2312' ) )
		{
			$keyWord = urldecode( $tmp[1] );
		}
		else
		{
			$keyWord = utf82gb2312( urldecode( $tmp[1] ) );
		}
	}
	else if( strstr( $domainName, 'lycos.com') )
	{
		preg_match( "|lycos.+query=([^\&]*)|is", $refererUrl, $tmp );
		$keyWord = urldecode( $tmp[1] );
	}
	else if( strstr( $domainName, '3721.com') )
	{
		preg_match( "|3721.+p=([^\&]*)|is", $refererUrl, $tmp );
		$keyWord = urldecode( $tmp[1] );
	}
	else if( strstr( $domainName, 'qq.com') )
	{
		preg_match( "|qq.+word=([^\&]*)|is", $refererUrl, $tmp );
		$keyWord = urldecode( $tmp[1] );
	}
	else if( strstr( $domainName, 'tom.com') )
	{
		preg_match( "|tom.+word=([^\&]*)|is", $refererUrl, $tmp );
		$keyWord = urldecode( $tmp[1] );
	}
	else if( strstr( $domainName, '21cn.com') )
	{
		preg_match( "|21cn.+word=([^\&]*)|is", $refererUrl, $tmp );
		$keyWord = urldecode( $tmp[1] );
	}
	else if( strstr( $domainName, 'sogou.com') )
	{
		preg_match( "|sogou.+query=([^\&]*)|is", $refererUrl, $tmp );
		$keyWord = urldecode( $tmp[1] );
	}
	else if( strstr( $domainName, 'aol.com') )
	{
		preg_match( "|aol.+query=([^\&]*)|is", $refererUrl, $tmp );
		$keyWord = urldecode( $tmp[1] );
	}

	$keyWord=str2utf8($keyWord,'gb2312');
	return  $keyWord;
}


/*
* ===========================================*
* 函数说明:获得时区
* ===========================================*
*/
function GetTimeZone( $TimeZone )
{
	$TimeZoneLists = array(
	'12' => array('GMT-12:00', '埃尼威托克岛，夸贾林岛'),
	'11' => array('GMT-11:00', '中途岛，东萨摩亚'),
	'10' => array('GMT-10:00', '夏威夷'),
	'9' => array('GMT-9:00', '阿拉斯加'),
	'8' => array('GMT-8:00', '太平洋时间（美国和加拿大），蒂华纳'),
	'7' => array('GMT-7:00', '山地时间（美国和加拿大），亚利桑那'),
	'6' => array('GMT-6:00', '中部时间（美国和加拿大），墨西哥城，特古西加尔巴，萨斯喀彻温省'),
	'5' => array('GMT-5:00', '东部时间（美国和加拿大），印第安那州（东部），波哥大，利马，基多'),
	'4' => array('GMT-4:00', '大西洋时间（加拿大），加拉加斯，拉巴斯'),
	'3.5' => array('GMT-3:30', '纽芬兰'),
	'3' => array('GMT-3:00', '巴西利亚，布宜诺斯艾利斯，乔治敦'),
	'2' => array('GMT-2:00', '中大西洋'),
	'1' => array('GMT-1:00', '亚速尔群岛，佛得角群岛'),
	'0' => array('GMT', '格林威治标准时间: 伦敦，都柏林，爱丁堡，里斯本，卡萨布兰卡，蒙罗维亚'),
	'-1' => array('GMT+01:00', '阿姆斯特丹，柏林，伯尔尼，罗马，斯德哥尔摩，维也纳，贝尔格莱德，布拉迪斯拉发，布达佩斯，卢布尔雅那，布拉格，布鲁赛尔，哥本哈根，马德里，巴黎，萨拉热窝，斯科普里，索非亚，华沙，萨格勒布'),
	'-2' => array('GMT+02:00', '布加勒斯特，哈拉雷，比勒陀尼亚，赫尔辛基，里加，塔林，开罗，雅典，伊斯坦布尔，明斯克，以色列'),
	'-3' => array('GMT+03:00', '巴格达，科威特，利雅得，莫斯科，圣彼得堡，伏尔加格勒，内罗毕'),
	'-3.5' => array('GMT+03:00', '德黑兰'),
	'-4' => array('GMT+04:00', '阿布扎比，马斯喀特，巴库，第比利斯'),
	'-4.5' => array('GMT+04:00', '喀布尔'),
	'-5' => array('GMT+05:00', '叶卡特琳堡，伊斯兰堡，卡拉奇，塔什干'),
	'-5.5' => array('GMT+05:00', '孟买，加尔各答，马德拉斯，新德里'),
	'-6' => array('GMT+06:00', '阿拉木图，达卡，科伦坡'),
	'-7' => array('GMT+07:00', '曼谷，河内，雅加达'),
	'-8' => array('GMT+08:00', '北京，重庆，广州，上海，香港，乌鲁木齐，台北，新加坡，佩思'),
	'-9' => array('GMT+09:00', '平壤，汉城，东京，大阪，札幌，雅库茨克'),
	'-9.5' => array('GMT+09:00', '阿得莱德，达尔文'),
	'-10' => array('GMT+10:00', '布里斯班，关岛，莫尔兹比港，霍巴特，堪培拉，墨尔本，悉尼'),
	'-11' => array('GMT+11:00', '马加丹，所罗门群岛，新喀里多尼亚'),
	'-12' => array('GMT+12:00', '奥克兰，惠灵顿，斐济，堪察加半岛，马绍尔群岛'),
	'-13' => array('GMT+13:00', '努库阿洛法'),
	'undefined' => array('未知', '未知'),
	);


	$TimeZone = trim( $TimeZone );
	$TimeZone = $TimeZone == '' ? '未知时区' : $TimeZone;
	if( $TimeZoneLists[$TimeZone] == '' )
	{
		return $TimeZone;
	}
	else
	{
		return $TimeZoneLists[$TimeZone];
	}
}


/*
* ===========================================*
* 函数说明:获取操作系统版本v:viewerInfo[HTTP_USER_AGENT]
* ===========================================*
*/
function GetSystem($v)
{
	if( $system = matchvar( $v, "|(Windows NT[\ 0-9\.]*)|i" ) );
	else if( $system = matchvar( $v, "|(Windows[\ 0-9\.]*)|i" ) );
	else if( $system = matchvar( $v, "|(Mac[^;^)]*)|i" ) );
	else if( $system = matchvar( $v, "|(unix)|i" ) );
	else if( $system = matchvar( $v, "|(Linux[\ 0-9\.]*)|i" ) );
	else if( $system = matchvar( $v, "|(SunOS[\ 0-9\.]*)|i" ) );
	else if( $system = matchvar( $v, "|(BSD[\ 0-9\.]*)|i" ) );
	else
	{
		$system = '其它';
	}
	return  trim( $system );
}

/*
* ===========================================*
* 函数说明:获取浏览器信息:viewerInfo[HTTP_USER_AGENT]
* ===========================================*
*/
function GetBrowser($v)
{
	if( $browser = matchvar( $v, "|(myie[^;^)^(]*)|i" ) );
	else if( $browser = matchvar( $v, "|(Netscape[^;^)^(]*)|i" ) );
	else if( $browser = matchvar( $v, "|(Opera[^;^)^(]*)|i" ) );
	else if( $browser = matchvar( $v, "|(NetCaptor[^;^^()]*)|i" ) );
	else if( $browser = matchvar( $v, "|(TencentTraveler)|i" ) );
	else if( $browser = matchvar( $v, "|(Firefox[0-9/\.^)^(]*)|i" ) );
	else if( $browser = matchvar( $v, "|(MSN[^;^)^(]*)|i" ) );
	else if( $browser = matchvar( $v, "|(Lynx[^;^)^(]*)|i" ) );
	else if( $browser = matchvar( $v, "|(Konqueror[^;^)^(]*)|i" ) );
	else if( $browser = matchvar( $v, "|(WebTV[^;^)^(]*)|i" ) );
	else if( $browser = matchvar( $v, "|(msie[^;^)^(]*)|i" ) );
	else if( $browser = matchvar( $v, "|(Chrome[0-9/\.^)^(]*)|i" ) );
	else if( $browser = matchvar( $v, "|(Safari[0-9/\.^)^(]*)|i" ) );
	else
	{
		$browser = '其它';
	}
	//_d($browser);
	return  trim( $browser );
}


/*
* ===========================================*
* 函数说明:对内容进行正则匹配,并返回内容
* ===========================================*
*/
function matchvar( $var, $reg )
{
	if( preg_match( $reg, $var, $tmp ) )
	{
		return $tmp[1];
	}
	else
	{
		return false;
	}
}


/*
* ===========================================*
* 函数说明:获得用户的语言
* ===========================================*
*/
function GetLanguage( $Language )
{
	$LanguageLists = array(
	'af' => 'Afrikaans',
	'sq' => 'Albanian',
	'zu' => 'Zulu',
	'ar-sa' => 'Arabic (Saudi Arabia)',
	'ar-iq' => 'Arabic (Iraq)',
	'xh' => 'Xhosa',
	'ar-eg' => 'Arabic (Egypt)',
	'ar-ly' => 'Arabic (Libya)',
	'ji' => 'Yiddish',
	'ar-dz' => 'Arabic (Algeria)',
	'ar-ma' => 'Arabic (Morocco)',
	've' => 'Venda',
	'ar-tn' => 'Arabic (Tunisia)',
	'ar-om' => 'Arabic (Oman)',
	'vi' => 'Vietnamese',
	'ar-ye' => 'Arabic (Yemen)',
	'ar-sy' => 'Arabic (Syria)',
	'uk' => 'Ukrainian',
	'ar-jo' => 'Arabic (Jordan)',
	'ar-lb' => 'Arabic (Lebanon)',
	'ur' => 'Urdu',
	'ar-kw' => 'Arabic (Kuwait)',
	'ar-ae' => 'Arabic (U.A.E.)',
	'tn' => 'Tswana',
	'ar-bh' => 'Arabic (Bahrain)',
	'ar-qa' => 'Arabic (Qatar)',
	'tr' => 'Turkish',
	'eu' => 'Basque',
	'bg' => 'Bulgarian',
	'th' => 'Thai',
	'be' => 'Belarusian',
	'ca' => 'Catalan',
	'ts' => '',
	'' => 'Tsonga',
	'zh-tw' => 'Chinese (Taiwan)',
	'zh-cn' => 'Chinese (PRC)',
	'sv' => 'Swedish',
	'zh-hk' => 'Chinese (Hong Kong, S.A.R. China)',
	'zh-sg' => 'Chinese (Singapore)',
	'sv-fi' => 'Swedish (Finland)',
	'hr' => 'Croatian',
	'cs' => 'Czech',
	'es-pr' => 'Spanish (Puerto Rico)',
	'da' => 'Danish',
	'nl' => 'Dutch (Standard)',
	'sx' => 'Sutu',
	'nl-be' => 'Dutch (Belgium)',
	'en' => 'English',
	'es-hn' => 'Spanish (Honduras)',
	'en-us' => 'English (United States)',
	'en-gb' => 'English (Great Britain)',
	'es-ni' => 'Spanish (Nicaragua)',
	'en-au' => 'English (Australia)',
	'en-ca' => 'English (Canada)',
	'es-bo' => 'Spanish (Bolivia)',
	'en-nz' => 'English (New Zealand)',
	'en-ie' => 'English (Ireland)',
	'es-sv' => 'Spanish (El Salvador)',
	'en-bz' => 'English (Belize)',
	'en-za' => 'English (South Africa)',
	'en-tt' => 'English (Trinidad)',
	'et' => 'Estonian',
	'en-jm' => 'English (Jamaica)',
	'fi' => 'Finnish',
	'fr' => 'French (Standard)',
	'fo' => 'Faeroese',
	'fr-be' => 'French (Belgium)',
	'fr-ca' => 'French (Canada)',
	'fa' => 'Farsi',
	'gd' => 'Gaelic (Scotland)',
	'gd-ie' => 'Gaelic (Ireland)',
	'fr-ch' => 'French (Switzerland)',
	'de' => 'German (Standard)',
	'de-ch' => 'German (Switzerland)',
	'fr-lu' => 'French (Luxembourg)',
	'de-li' => 'German (Liechtenstein)',
	'el' => 'Greek',
	'de-at' => 'German (Austria)',
	'he' => 'Hebrew',
	'hi' => 'Hindi',
	'de-lu' => 'German (Luxembourg)',
	'in' => 'Indonesian',
	'it' => 'Italian (Standard)',
	'hu' => 'Hungarian',
	'it-ch' => 'Italian (Switzerland)',
	'ja' => 'Japanese',
	'is' => 'Icelandic',
	'lv' => 'Latvian',
	'lt' => 'Lithuanian',
	'ko' => 'Korean',
	'mk' => 'FYRO Macedonian',
	'ms' => 'Malaysian',
	'ko' => 'Korean (Johab)',
	'no' => 'Norwegian (Nynorsk)',
	'pl' => 'Polish',
	'mt' => 'Maltese',
	'pt-br' => 'Portuguese (Brazil)',
	'pt' => 'Portuguese (Standard)',
	'no' => 'Norwegian (Bokmal)',
	'ro-mo' => 'Romanian (Moldavia)',
	'ru' => 'Russian',
	'rm' => 'Rhaeto-Romanic',
	'ru-mo' => 'Russian (Moldavia)',
	'sz' => 'Sami (Lappish)',
	'ro' => 'Romanian',
	'sk' => 'Slovak',
	'sl' => 'Slovenian',
	'sr' => 'Serbian (Cyrillic)',
	'sb' => 'Sorbian',
	'es' => 'Spanish (Spain C Traditional)',
	'sr' => 'Serbian (Latin)',
	'es-gt' => 'Spanish (Guatemala)',
	'es-cr' => 'Spanish (Costa Rica)',
	'es-mx' => 'Spanish (Mexico)',
	'es-pa' => 'Spanish (Panama)',
	'es-do' => 'Spanish (Dominican Republic)',
	'es' => 'Spanish (Spain C Modern)',
	'es-pe' => 'Spanish (Peru)',
	'es-ar' => 'Spanish (Argentina)',
	'es-ve' => 'Spanish (Venezuela)',
	'es-ec' => 'Spanish (Ecuador)',
	'es-cl' => 'Spanish (Chile)',
	'es-co' => 'Spanish (Colombia)',
	'es-uy' => 'Spanish (Uruguay)',
	'es-py' => 'Spanish (Paraguay)',
	'undefined' => '<i>δ?</i>'
	);

	$Language = strtolower( trim( $Language ) );
	$Language = $Language == '' ? 'undefined' : $Language;
	if( $LanguageLists[$Language] == '' )
	{
		return $Language;
	}
	else
	{
		return $LanguageLists[$Language];
	}
}

/*
* ===========================================*
* 函数说明:输出格式化后的内容
* ===========================================*
*/
function _out ( $content, $type = 'html', $exit = true, $ret = false )
{
	global $TPL_IMGURL;

	$type = strtolower ( $type );
	$type = $type == 'js' ? 'js' : 'html';

	$content = str_replace ( TPL_IMGDIR, $TPL_IMGURL, $content );

	if ( $type == 'js' )
	{
		$content = preg_replace ( "/\/\/(.+)/", '', $content );
		$content = str_replace ( "\n", '', $content );
		$content = str_replace ( "\r", '', $content );
		$content = str_replace ( '\'', '\\\'', $content );
		$content = "document.write ( '" . $content . "' )";
	}

	//$conn->close ();

	if( $ret )
	{
		return $content;
	}
	else
	{
		echo $content;
	}

	if( $exit == true )
	{
		ob_end_flush();
		exit;
	}
}


/*
* ===========================================*
* 函数说明:初始化
* ===========================================*
*/
function initialize( &$varArray, $varNames )
{
	if( is_array( $varArray ) && count( $varArray ) > 0 )
	{
		foreach( $varArray as $key=>$varValue )
		{
			if( isset( $varNames[$key] ) )
			{
				$varArray[$key] = $varNames[$key] ( $varValue );
			}
			else
			{
				unset( $varArray[$key] );
			}
		}
	}
}


/*
* ===========================================*
* 函数说明:简单debug
* ===========================================*
*/
function _d( $content )
{
	echo "<pre>";
	print_r( $content );
	echo "</pre>";
}

function echojs( $content )
{
	echo 'document.write( "' . addslashes( $content ) . "\" );\n";
}


/*
* ===========================================*
* 函数说明:UTF8 to GB2312
* ===========================================*
*/
function utf82gb2312( $SourceText )
{
	$Out=mb_convert_encoding($SourceText, "UTF-8", "GB2312");
	return $Out;
}


/*
* ===========================================*
* 函数说明:charset to UTF8
* ===========================================*
*/
function str2utf8( $SourceText,$charset )
{
	$Out=mb_convert_encoding($SourceText,$charset, "UTF-8");
	return $Out;
}


/*
* ===========================================*
* 函数说明:rmDir
* ===========================================*
*/
function _rmDir($dirName)
{
	$result = false;
	if(!is_dir($dirName))
	{
		return false;
	}

	$handle = opendir($dirName);
	while(($file = readdir($handle)) !== false)
	{
		if($file != '.' && $file != '..')
		{
			$dir = $dirName . DIRECTORY_SEPARATOR . $file;
			is_dir($dir) ? _RmDir($dir) : unlink($dir);
		}
	}
	closedir($handle);

	$result = rmdir($dirName) ? true : false;

	return $result;
}

/*
* ===========================================*
* 函数说明:格式化空间容量
* ===========================================*
*/
function format_size ( $in )
{
	if ( $in >= 1024 * 1024 * 1024 )
	{
		$out = sprintf ( "%01.2f", $in / ( 1024 * 1024 * 1024 ) ) . " GB";
	}
	elseif ( $in >= 1024 * 1024 )
	{
		$out = sprintf ( "%01.2f", $in / ( 1024 * 1024 ) ) . " MB";
	}
	elseif ( $in >= 1024 )
	{
		$out = sprintf ( "%01.2f", $in / 1024 ) . " KB";
	}
	else
	{
		$out = $in . " Bytes";
	}
	return ( $out );
}

/*
* ===========================================*
* 函数说明:写入文件
* ===========================================*
*/
function write_to_file ($filename, $data, $method = "w")
{
	$fp = @fopen($filename, $method);
	if(!$fp)
	{
		return 0;
	}
	flock($fp, LOCK_EX);
	$opt = @fwrite($fp, $data);
	fclose($fp);
	return $opt;
}

/*
* ===========================================*
* 函数说明:读取文件
* ===========================================*
*/
function read_from_file ( $file )
{
	if ( ! file_exists ( $file ) )
	{
		return ( FALSE );
	}

	$fp = fopen ( $file, "r" );
	if ( ! $fp )
	{
		return ( FALSE );
	}

	flock ( $fp, LOCK_SH );
	$data = fread ( $fp, filesize ( $file ) );
	fclose ( $fp );
	return ( $data );
}

/*
* 函数说明:ip转化为数字
*/
function ip2num( $ip )
{
	$ipData = explode( '.', $ip );
	if( count( $ipData ) != 4 )
	{
		return 0;
	}
	$ipNum = 0;
	foreach( $ipData as $key => $val )
	{
		$ipNum += intval( $val ) * pow ( 256, 3-$key );
	}
	return $ipNum;
}

?>